import { defineStore } from 'pinia';

// 新增商户表单
export const useFormStore = defineStore('form', {
  state: () => {
    return {
      merchantForm: {
        merchantType: '', //企业类型
        cooperateType: '', //合作类型
        belongService: '', //归属服务商
        belongCity: '', //归属地市
        merchantName: '', //商户名称
        nationality: '', //国籍
        userName: '', //姓名
        idType: '', //证件类型
        idNum: '', //证件号
        phone: '', //联系电话
        bankCardNum: '', //银行卡号
        idFaceImage: '', //人脸面
        idNationalImage: '', //国徽面
        merchantInfoImage: '', //营业执照
      },

      brandForm: {
        merchantId: uni.getStorageSync('merchantId') || '', //商户id
        brandNum: '', //品牌编码
        brandType: '', //品牌类型
        brandName: '', //名称
        brandTitle: '', //标题
        brandIntroduction: '', //介绍
        brandLogoImage: '', //logo图
        brandImage: '', //品牌图
        standbyVerifyAccount: '', //备用手机号
        brandInfoImage: '', //营业执照
        shopList: [], // 店铺列表
      },

      currentShopId: -1,
      currentGoodsId: -1,

      shopForm: {
        // merchantId: -1, //商户id
        brandId: '', //品牌id
        shopName: '', //名称
        shopType: '', //店铺类型
        shopAddress: '', //地址
        industryType: '', //行业类型
        avgPrice: '', //人均消费
        businessTime: '', //营业时间
        standbyVerfiyAccount: '', //备用核销账号
        shopIntroduction: '', //介绍
        shopScore: '', //评分
        shopPhone: '', //电话
        shopImage: '', //店铺图片
        shopInfoImage: '', //营业执照/店铺授权书
        goodsList: [], // 商品列表
      },

      goodsForm: {
        brandId: '', //品牌id
        shopId: '', //店铺id
        goodsName: '', //商品名称
        goodsType: '', //商品类型(1自制券 2线上券3第三方接口券4实物商品)
        stock: 0, //库存
        validTimeType: '', //有效期类型(1.长久有效 2.自定义期限 3.当天有效)
        validTime: '', //时间
        receiveValidDay: '', //领取后有效天数
        useScope: '', //适用范围
        limitUseCount: '', //限制使用次数
        useThreshold: '', //使用门槛值 分
        parPrice: '', //面值 分
        marketPrice: '', //市场价 分
        salePrice: '', //售价 分
        goodsDetail: '', //商品详情
        useProcess: '', //使用流程
        goodsImage: '', //商品图片
      },
      couponForm: {
        couponConfigs: [
          // {
          //   goodsId: 1, //商品id
          // },
          // {
          //   goodsId: 2, //商品id
          // },
        ],
        brandId: '', //品牌id
        shopId: '', //店铺id
        couponName: '', //券包名称
        couponType: '', //券类型
        stock: '', //库存
        validTimeType: '', //有效期类型(1.长久有效 2.自定义期限 3.当天有效)
        validTime: '', //时间
        receiveValidDay: '', //领取后有效天数
        useScope: '', //适用范围
        limitUseCount: '', //限制使用次数
        useThreshold: '', //使用门槛值 分
        parPrice: '', //面值 分
        marketPrice: '', //市场价 分
        salePrice: '', //售价 分
        couponDetail: '', //详情
        useProcess: '', //使用流程
        couponImage: '', //图片
      },

      relateGoodsId: [], // 券包关联的商品id
    };
  },

  getters: {
    shopList(state) {
      return state.brandForm.shopList;
    },
    // currentGoodsList(state) {
    //   let currentShop = this.brandForm.shopList.find((item) => item.sid == this.currentShopId);
    //   if (currentShop) {
    //     return currentShop.goodsList || [];
    //   } else {
    //     return [];
    //   }
    // },
  },
  actions: {
    currentGoodsList(mode) {
      if (mode == 'home') {
        let currentShop = this.shopForm;
        if (currentShop) {
          return currentShop.goodsList;
        } else {
          return [];
        }
      } else {
        let currentShop = this.brandForm.shopList.find((item) => item.sid == this.currentShopId);
        if (currentShop) {
          return currentShop.goodsList;
          return [];
        }
      }
    },
    resetBrandForm() {
      this.brandForm = {
        merchantId: uni.getStorageSync('merchantId') || '', //商户id
        brandNum: '', //品牌编码
        brandType: '', //品牌类型
        brandName: '', //名称
        brandTitle: '', //标题
        brandIntroduction: '', //介绍
        brandLogoImage: '', //logo图
        brandImage: '', //品牌图
        standbyVerifyAccount: '', //备用手机号
        brandInfoImage: '', //营业执照
        shopList: [], // 店铺列表
      };

      this.currentShopId = -1;
      this.currentGoodsId = -1;
    },

    resetShopForm() {
      this.shopForm = {
        brandId: '', //品牌id
        shopName: '', //名称
        shopType: '', //店铺类型
        shopAddress: '', //地址
        industryType: '', //行业类型
        avgPrice: '', //人均消费
        businessTime: '', //营业时间
        standbyVerfiyAccount: '', //备用核销账号
        shopIntroduction: '', //介绍
        shopScore: '', //评分
        shopPhone: '', //电话
        shopImage: '', //店铺图片
        shopInfoImage: '', //营业执照/店铺授权书
        goodsList: [], // 商品列表
      };
    },

    resetGoodsForm() {
      this.goodsForm = {
        brandId: '', //品牌id
        shopId: '', //店铺id
        goodsName: '', //商品名称
        goodsType: '', //商品类型(1自制券 2线上券3第三方接口券4实物商品)
        stock: 0, //库存
        validTimeType: '', //有效期类型(1.长久有效 2.自定义期限 3.当天有效)
        validTime: '', //时间
        receiveValidDay: '', //领取后有效天数
        useScope: '', //适用范围
        limitUseCount: '', //限制使用次数
        useThreshold: '', //使用门槛值 分
        parPrice: '', //面值 分
        marketPrice: '', //市场价 分
        salePrice: '', //售价 分
        goodsDetail: '', //商品详情
        useProcess: '', //使用流程
        goodsImage: '', //商品图片
      };
    },

    // 重置卡券
    resetCouponForm() {
      this.couponForm = {
        couponConfigs: [],
        brandId: '', //品牌id
        shopId: '', //店铺id
        couponName: '', //券包名称
        couponType: '', //券类型
        stock: '', //库存
        validTimeType: '', //有效期类型(1.长久有效 2.自定义期限 3.当天有效)
        validTime: '', //时间
        receiveValidDay: '', //领取后有效天数
        useScope: '', //适用范围
        limitUseCount: '', //限制使用次数
        useThreshold: '', //使用门槛值 分
        parPrice: '', //面值 分
        marketPrice: '', //市场价 分
        salePrice: '', //售价 分
        couponDetail: '', //详情
        useProcess: '', //使用流程
        couponImage: '', //图片
      };

      this.relateGoodsId = [];
    },

    // 新增商品
    addGoodsByShopId(mode) {
      if (mode == 'home') {
        let currentShop = this.shopForm;
        let goodsId = new Date().getTime();
        if (currentShop) {
          this.currentGoodsId = goodsId;
          if (!currentShop.goodsList) {
            currentShop.goodsList = [];
          }
          currentShop.goodsList.push({
            gid: goodsId,
          });
        } else {
          console.log('找不到店铺');
        }
      } else {
        let currentShop = this.brandForm.shopList.find((item) => item.sid == this.currentShopId);
        let goodsId = new Date().getTime();
        if (currentShop) {
          this.currentGoodsId = goodsId;

          if (!currentShop.goodsList) {
            currentShop.goodsList = [];
          }
          currentShop.goodsList.push({
            gid: goodsId,
          });
        } else {
          console.log('找不到店铺');
        }
      }
    },
    // 更新店铺
    updateShopById(id, shopData, mode) {
      if (mode == 'home') {
        let currentShop = this.shopForm;
        Object.assign(currentShop, shopData);

        console.log(this.shopForm, 'shopform <<<<');
      } else {
        let currentShop = this.brandForm.shopList.find((item) => item.sid == id);
        if (currentShop) {
          Object.assign(currentShop, shopData);

          console.log(currentShop, 'currenshop >>>>>>>>');
        } else {
          console.log('更新店铺  找不到店铺');
        }
      }
    },
    // 更新商品
    updateGoodsById(goodsData, mode) {
      // 创建商品
      if (mode == 'home') {
        let currentGoods = this.goodsForm;
        if (currentGoods) {
          Object.assign(currentGoods, goodsData);
        } else {
          console.log('更新商品  找不到商品');
        }
      } else {
        console.log(this.shopForm.brandId, 'this.shopForm.brandId >>>>>');
        if (this.shopForm.brandId) {
          // 创建店铺 - 创建商品
          let currentShop = this.shopForm;
          let currentGoods = currentShop.goodsList.find((item) => item.gid == this.currentGoodsId);
          if (currentGoods) {
            Object.assign(currentGoods, goodsData);
          } else {
            console.log('更新商品  找不到商品');
          }
        } else {
          // 创建品牌 - 创建店铺 - 创建商品
          let currentShop = this.brandForm.shopList.find((item) => item.sid == this.currentShopId);
          let currentGoods = currentShop.goodsList.find((item) => item.gid == this.currentGoodsId);
          if (currentGoods) {
            Object.assign(currentGoods, goodsData);
            // this.currentGoodsId = -1;
          } else {
            console.log('更新商品  找不到商品');
          }
        }
      }
    },

    // 更新券包
    addCoupon(data) {
      Object.assign(this.couponForm, data);
    },
  },
});
